﻿using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 多点移动
/// </summary>
public class MoveAction : IMoveAction
{
    private Vector3 targetPos = Vector3.zero;

    public MoveAction(OwnerObj ownerObj) : base(ownerObj)
    {

    }

    public override bool IsCanInterrupt(IAction nextAction)
    {
        return true;
    }

    public override void SetPath(List<Vector3> path)
    {
        if (path.Count > 0)
        {
            targetPos = path[0];
        }
    }

    public override void Unload()
    {
        UnityGameObject unityGameObject = ownerObj.Get<IUnitObject>().GetUnityGameObject();
        if (null != unityGameObject)
        {
            unityGameObject.SetAnmiBool("Run", false);
        }
    }

    public override void Update(float deltaTime)
    {
        if (finished)
        {
            return;
        }

        UnityGameObject unityGameObject = ownerObj.Get<IUnitObject>().GetUnityGameObject();
        if (null == unityGameObject)
        {
            return;
        }

        ModuleManager modManager = ownerObj.Get<IUnitObject>().GetModule();
        ModAttr modAttr = modManager.Get<ModAttr>();
        float speed = modAttr.GetSpeed();

        Vector3 curPosition = unityGameObject.GetPosition();
        if (Vector3.Distance(targetPos, curPosition) < 0.1f)
        {
            unityGameObject.SetAnmiBool("Run", false);

            // 到达目标点
            finished = true;
        }
        else
        {
            unityGameObject.SetAnmiBool("Run", true);

            // 位置变化计算
            curPosition += (targetPos - curPosition).normalized * deltaTime * speed;

            // 更新位置
            unityGameObject.SetPosition(curPosition);

            if (curPosition.x < targetPos.x)
            {
                unityGameObject.SetRotation(Quaternion.Euler(0, 180, 0));
            }
            else
            {
                unityGameObject.SetRotation(Quaternion.Euler(0, 0, 0));
            }
        }
    }
}